package com.designPattern.observer.dns;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 浏览器
 * @author : liutianlong
 * @date : 2023/6/2
 * @since : 1.0
 */
public class Browser {

    public static void main(String[] args) {
        // 创建三个dns服务
        TopServer topServer = new TopServer();
        ChinaServer chinaServer = new ChinaServer();
        ShangHaiServer shangHaiServer = new ShangHaiServer();
        // 上海的上级为中国
        shangHaiServer.addUpperServer(chinaServer);
        // 中国的服务器上级为顶级dns服务器
        chinaServer.addUpperServer(topServer);
        //解析域名
        System.out.println("=====域名解析模拟器=====");

        while(true) {
            System.out.print("\n请输入域名(输入N退出):");
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            try {
                String domain = reader.readLine();
                if("n".equalsIgnoreCase(domain)) {
                    break;
                }
                Recorder recorder = new Recorder();
                recorder.setDomain(domain);
                shangHaiServer.update(null,recorder);
                System.out.println("----DNS服务器解析结果----");
                System.out.println(recorder);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
